// pages/noticeMore/noticeMore.js
const db = wx.cloud.database();
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    administration:{},
    fontSize:"17px",
    font:"改大号",
    scrollTop:"comment",
        //收藏数据
        collectionUrl:'',
        isClick:false,
        openid:'',
        flag:false,
        allcollectionList:[],
        currentUrl:'',
        collectionTitle:'',
        title:"政策法规"
  },

  fontChange(){
    this.setData({
      fontSize:this.data.fontSize==="17px"?"25px":"17px",
      font:this.data.font==="改大号"?"改默认":"改大号",
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
     //跳转时的路径参数带有comment为true，则跳到留言区，用的是scroll-view的锚点定位
    if(options.comment){
      setTimeout(()=>{
        this.setData({
          scrollTop:"comment",
        })
      },1200)
    }
    this.setData({
      currentUrl:options._id,
      id:options._id
    })
    db.collection("administration_moreList").doc(options._id).get({
      success:(res)=>{
        this.setData({
          administration: res.data,
        })
      }
    })
    this.checkCollection(); 
  },
  checkCollection(){
      let that=this;
     db.collection('collectionurlList').where({
       _openid:this.data.openid
     }).get({
       success(res){
         that.setData({
          allcollectionList:res.data
         })
       }
     })
setTimeout(()=>{
  for(var i=0;i<that.data.allcollectionList.length;i++){
    if(this.data.allcollectionList[i].collectionUrl==this.data.currentUrl){
      console.log(this.data.allcollectionList[i].collectionUrl);
      console.log(this.data.currentUrl);
      console.log("就是他");
      this.setData({
        isClick:true
      })
    }
  }
},1000)
  },
  //收藏
haveSave(e){
  //点击收藏
  if(!this.data.isClick == true){
    this.setData({
      collectionUrl:`${this.data.currentUrl}`,
      collectionTitle:this.data.administration.title,
      isClick:true
    })
    let that=this;
    db.collection('collectionurlList').where({
      _openid:app.globalData.openid,
      collectionUrl:that.data.collectionUrl
    }).get({
      success(res){
        if(res.data.length==0){
          db.collection('collectionurlList').add({
            data:{
              type:that.data.title,
          collectionUrl:that.data.collectionUrl,
          collectionTitle:that.data.collectionTitle
        }
      })
        }
        else{}
      }
    })
    wx.showToast({
      title: '已收藏',
    });
  }else{
    this.setData({
      collectionUrl: '',
      isClick:false
    })
    db.collection('collectionurlList').where({
      _openid:this.data.openid,
      collectionUrl:this.data.currentUrl,
    }).remove({
      success(res){
        console.log(res);
      }
    })
    wx.showToast({
      title: '已取消收藏',
    })
  }
  console.log(this.data.collectionUrl);
},
  //到底时让评论拉取更多数据
  onReachBottom(){
    this.selectComponent('.comment').onReachBottom()
  },
  onShareAppMessage() {},
  onShareTimeline() {},
  onAddToFavorites() {},
  
})